<?php
/**
 * 网站地图生成插件
 *
 * 可单独 生成移动端或者电脑端网站地图
 *
 * author：sitemapv2@163.com
 *
 * time:2020-11-23
 */

namespace plugins\sitemapv2\controller;

use cmf\controller\PluginAdminBaseController;
use app\portal\model\PortalCategoryModel;
use app\portal\model\PortalPostModel;
use think\Db;

class AdminIndexController extends PluginAdminBaseController
{
    protected function _initialize()
    {
        parent::_initialize();
        $adminId = cmf_get_current_admin_id();
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        }
    }

    public function index()
    {
        $request = $this->request;
        $host = $request->domain();
        $mobile = str_replace("www", "m", $host);
        $this->assign("mobile", $mobile);
        $this->assign("host", $host);
        return $this->fetch('/admin_index');
    }

    /*电脑端生成xml*/
    public function addxml()
    {
        ini_set('memory_limit', '512M');
        $site = 'http://' . $_SERVER['HTTP_HOST'];
        $html = '<a href="' . $site . '/">首页</a>';
        $lastmod = date('Y-m-d H:i:s', time());
        $xml = '<?xml version="1.0" encoding="utf-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0"><url><loc>' . $site . '/</loc><changefreq>daily</changefreq><priority>1</priority><lastmod>' . $lastmod . '</lastmod></url>';

        // 输出分类
        $portalCategoryModel = new PortalCategoryModel();
        $category = $portalCategoryModel->where('status', 1)->where('delete_time', 0)->select();
        foreach ($category as $val) {
            $lastmod = date('Y-m-d H:i:s', time());
            $geturl = $site . cmf_url('portal/List/index', 'id=' . $val['id']);
            $html .= '<a href="' . $geturl . '">' . $val['name'] . '</a>' . "<br>";
            $xml .= '<url><loc>' . $geturl . '</loc><changefreq>daily</changefreq><priority>0.8</priority><lastmod>' . $lastmod . '</lastmod></url>';
        }

        $xml .= '</urlset>';
        $this->updatexml($xml, $pagess = 1);
        //首页，栏目，页面生成成功之后，生成文章页面
        $this->article_sitemap();
        $this->success('生成xml成功！');
    }

    public function article_sitemap()
    {
        set_time_limit(36000);
        ini_set('memory_limit', '512M');
        $PortalPostModel = new PortalPostModel();
        $site = 'http://' . $_SERVER['HTTP_HOST'];
        $html = '<a href="' . $site . '/">首页</a>';
        $lastmod = date('Y-m-d H:i:s', time());

        // 输出文章
        $where['post_status'] = '1';
        $where['post_type'] = '1';
        $article = $PortalPostModel->where($where)->where('delete_time', 0)->count();
        /*建立几个xml文件*/
        $chaxun_count = ceil($article / 5000);
//        生成主文件  sitemap.xml

        $xmlz = '<?xml version="1.0" encoding="utf-8" ?><sitemapindex>';
        for ($j = 0; $j <= $chaxun_count; $j++) {
            if ($j <= $chaxun_count + 1) {
                $xmlz .= '<sitemap><loc>' . $site . '/sitemap-' . ($j + 1) . "." . "xml" . '</loc><lastmod>' . date('Y-m-d', time()) . '</lastmod></sitemap>';
            }
        }
        $xmlz .= '</sitemapindex>';
        $this->updatexml($xmlz, $pagess = '');
        /*生成文章文件*/
        for ($i = 1; $i <= $chaxun_count; $i++) {
            if ($i == 1) {
                $article = $PortalPostModel->where($where)->where('delete_time', 0)->limit(1, 5000)->select();
                /*第一次取5000条数据*/
                $xml = '<?xml version="1.0" encoding="utf-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0"><url><loc>' . $site . '/</loc><changefreq>daily</changefreq><priority>1</priority><lastmod>' . $lastmod . '</lastmod></url>';
                foreach ($article as $value) {
                    $lastmod = date('Y-m-d H:i:s', $value['create_time']);
					
					// 循环输出文章
                    $geturl = $site . cmf_url('portal/Article/index', array('id' => $value['id'], 'cid' => $value['category_id']));
					$html .= '<a href="' . $geturl . '">' . $value['post_title'] . '</a>' . "<br>";
					$xml .= '<url><loc>' . $geturl . '</loc><changefreq>monthly</changefreq><priority>0.5</priority><lastmod>' . $lastmod . '</lastmod></url>';
                }
                $xml .= '</urlset>';
                $this->updatexml($xml, $pagess = $i + 1);
            } else {
                $xml = '<?xml version="1.0" encoding="utf-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0"><url><loc>' . $site . '/</loc><changefreq>daily</changefreq><priority>1</priority><lastmod>' . $lastmod . '</lastmod></url>';
                $article = $PortalPostModel->where($where)->where('delete_time', 0)->limit(5000 * ($i - 1), 5000)->select();
                foreach ($article as $value) {
                    $lastmod = date('Y-m-d H:i:s', $value['create_time']);

                    // 循环输出文章
					$geturl = $site . cmf_url('portal/Article/index', array('id' => $value['id'], 'cid' => $value['category_id']));
					$html .= '<a href="' . $geturl . '">' . $value['post_title'] . '</a>' . "<br>";
					$xml .= '<url><loc>' . $geturl . '</loc><changefreq>monthly</changefreq><priority>0.5</priority><lastmod>' . $lastmod . '</lastmod></url>';
                }
                $xml .= '</urlset>';
                $this->updatexml($xml, $pages = $i + 1);
            }
            if ($i == $chaxun_count) {
                $this->success('生成成功！');
            }
        }
    }

    /*手机端生成xml*/

    public function addxml2()
    {
        ini_set('memory_limit', '512M');
        $request = $this->request;
        $host = $request->domain();
        $site = str_replace("www", "m", $host);
        $html = '<a href="' . $site . '/">首页</a>';
        $lastmod = date('Y-m-d H:i:s', time());
        $xml = '<?xml version="1.0" encoding="utf-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0"><url><loc>' . $site . '/</loc><changefreq>daily</changefreq><priority>1</priority><lastmod>' . $lastmod . '</lastmod></url>';

        // 输出分类
        $portalCategoryModel = new PortalCategoryModel();
        $category = $portalCategoryModel->where('status', 1)->where('delete_time', 0)->select();
        foreach ($category as $val) {
            $lastmod = date('Y-m-d H:i:s', time());
            $geturl = $site . cmf_url('portal/List/index', 'id=' . $val['id']);
            $html .= '<a href="' . $geturl . '">' . $val['name'] . '</a>' . "<br>";
            $xml .= '<url><loc>' . $geturl . '</loc><changefreq>daily</changefreq><priority>0.8</priority><lastmod>' . $lastmod . '</lastmod></url>';
        }

        $xml .= '</urlset>';
        $this->updatexml2($xml, $pagess = 1);
        //首页，栏目，页面生成成功之后，生成文章页面
        $this->article_sitemap2();
        $this->success('生成xml成功！');
    }

    public function article_sitemap2()
    {
        set_time_limit(36000);
        ini_set('memory_limit', '512M');
        $PortalPostModel = new PortalPostModel();
        $request = $this->request;
        $host = $request->domain();
        $site = str_replace("www", "m", $host);
        $html = '<a href="' . $site . '/">首页</a>';
        $lastmod = date('Y-m-d H:i:s', time());

        // 输出文章
        $where['post_status'] = '1';
        $where['post_type'] = '1';
        $article = $PortalPostModel->where($where)->where('delete_time', 0)->count();
        /*建立几个xml文件*/
        $chaxun_count = ceil($article / 5000);
//        生成主文件  sitemap.xml

        $xmlz = '<?xml version="1.0" encoding="utf-8" ?><sitemapindex>';
        for ($j = 0; $j <= $chaxun_count; $j++) {
            if ($j <= $chaxun_count + 1) {
                $xmlz .= '<sitemap><loc>' . $site . '/sitemap_m-' . ($j + 1) . "." . "xml" . '</loc><lastmod>' . date('Y-m-d', time()) . '</lastmod></sitemap>';
            }
        }
        $xmlz .= '</sitemapindex>';
        $this->updatexml2($xmlz, $pagess = '');
        /*生成文章文件*/
        for ($i = 1; $i <= $chaxun_count; $i++) {
            if ($i == 1) {
                $article = $PortalPostModel->where($where)->where('delete_time', 0)->limit(1, 5000)->select();
                /*第一次取5000条数据*/
                $xml = '<?xml version="1.0" encoding="utf-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0"><url><loc>' . $site . '/</loc><changefreq>daily</changefreq><priority>1</priority><lastmod>' . $lastmod . '</lastmod></url>';
                foreach ($article as $value) {
                    $lastmod = date('Y-m-d H:i:s', $value['create_time']);

                    // 循环输出文章
					$geturl = $site . cmf_url('portal/Article/index', array('id' => $value['id'], 'cid' => $value['category_id']));
					$html .= '<a href="' . $geturl . '">' . $value['post_title'] . '</a>' . "<br>";
					$xml .= '<url><loc>' . $geturl . '</loc><changefreq>monthly</changefreq><priority>0.5</priority><lastmod>' . $lastmod . '</lastmod></url>';
                }
                $xml .= '</urlset>';
                $this->updatexml2($xml, $pagess = $i + 1);
            } else {
                $xml = '<?xml version="1.0" encoding="utf-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0"><url><loc>' . $site . '/</loc><changefreq>daily</changefreq><priority>1</priority><lastmod>' . $lastmod . '</lastmod></url>';
                $article = $PortalPostModel->where($where)->where('delete_time', 0)->limit(5000 * ($i - 1), 5000)->select();
                foreach ($article as $value) {
                    $lastmod = date('Y-m-d H:i:s', $value['create_time']);

                    // 循环输出文章
					$geturl = $site . cmf_url('portal/Article/index', array('id' => $value['id'], 'cid' => $value['category_id']));
					$html .= '<a href="' . $geturl . '">' . $value['post_title'] . '</a>' . "<br>";
					$xml .= '<url><loc>' . $geturl . '</loc><changefreq>monthly</changefreq><priority>0.5</priority><lastmod>' . $lastmod . '</lastmod></url>';
                }
                $xml .= '</urlset>';
                $this->updatexml2($xml, $pages = $i + 1);
            }
            if ($i == $chaxun_count) {
                $this->success('生成成功！');
            }
        }
    }

    /*电脑端生成xml文件*/
    private function updatexml($data, $pagess)
    {
        $this->writefile('xml', $data, $pagess);
    }

    private function writefile($type, $data, $pgess)
    {
        if (empty($pgess)) {
            $str = '/sitemap';
        } else {
            $str = '/sitemap-';
        }
        $myfile = fopen($_SERVER['DOCUMENT_ROOT'] . $str . $pgess . "." . $type, "w") or die("Unable to open file!");
        fwrite($myfile, $data);
        fclose($myfile);
    }

    /*手机端生成xml文件*/
    private function updatexml2($data, $pagess)
    {
        $this->writefile2('xml', $data, $pagess);
    }

    private function writefile2($type, $data, $pgess)
    {
        if (empty($pgess)) {
            $str = '/sitemap_m';
        } else {
            $str = '/sitemap_m-';
        }
        $myfile = fopen($_SERVER['DOCUMENT_ROOT'] . $str . $pgess . "." . $type, "w") or die("Unable to open file!");
        fwrite($myfile, $data);
        fclose($myfile);
    }


}
